package com.huibao.hulibao.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.huibao.hulibao.R;
import com.huibao.hulibao.activity.StartHuliActivity;
import com.huibao.hulibao.entity.HuLiBindMomEntity;
import com.huibao.hulibao.entity.HuLiEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class StartHuliPopNextLvAdapter extends BaseAdapter {
    public ArrayList<HuLiEntity.HuLiDetailData> mDatas;
    private Context mContext;
    private LayoutInflater mInflater;
    private StartHuliActivity startHuliActivity;
    //private String selected_data;
    private int mPosition;


    public StartHuliPopNextLvAdapter(ArrayList<HuLiEntity.HuLiDetailData> datas, Context context, StartHuliActivity startHuliActivity) {
        this.mDatas = datas;
        this.mContext = context;
        this.startHuliActivity = startHuliActivity;
        mInflater = LayoutInflater.from(context);
        //this.selected_data = selected_data;
        // this.mPosition = position;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        final HuLiEntity.HuLiDetailData hd = mDatas.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_pop_start_huli_next, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 设置TAG
        holder.et_beizhu.setTag(position);
        holder.et_beizhu.clearFocus();
        holder.tv_content.setText(hd.n);
        // LogUtils.d("selected_str = " + hd.selected_str + "position = " + position);
        // 显示选中的信息
        if (hd.selected_str != null && !hd.selected_str.equals("")) {
            holder.tv_selected_content.setText(hd.selected_str);

        } else {
            holder.tv_selected_content.setText("");
        }

        // 必选红星的判断
        if (hd.r.equals("1")) {
            holder.tv_is_bixu.setVisibility(View.VISIBLE);
        } else if (hd.r.equals("0")) {
            holder.tv_is_bixu.setVisibility(View.INVISIBLE);
        }

        // 单选和多选和文本的区别
        if (hd.t.equals("rad") || hd.t.equals("chb")) {
            holder.tv_selected_content.setVisibility(View.VISIBLE);
            holder.iv_enter.setVisibility(View.VISIBLE);
            holder.et_beizhu.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_selected_content.setVisibility(View.GONE);
            holder.iv_enter.setVisibility(View.INVISIBLE);
            holder.et_beizhu.setVisibility(View.VISIBLE);

            if (hd.input_data != null) {
                holder.et_beizhu.setText(hd.input_data);
            }

//            // 设置文本输入监听
            holder.et_beizhu.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    int pos = (int) holder.et_beizhu.getTag();
                    HuLiEntity.HuLiDetailData hd = mDatas.get(pos);
                    hd.input_data = charSequence.toString();
                }

                @Override
                public void afterTextChanged(Editable editable) {
//                    LogUtils.d("position = " + position);
//                    LogUtils.d("input_data = " + editable.toString());
                }
            });

            if (!TextUtils.isEmpty(hd.input_data)) {
                holder.et_beizhu.setText(hd.input_data);
            } else {
                holder.et_beizhu.setText("");
            }

        }

        // 文本输入类型的判断,数字文本
        if (hd.vt.equals("flo")) {
            holder.et_beizhu.setInputType(8194);
        }
        return convertView;
        // return v;
    }


    static class ViewHolder {
        @BindView(R.id.tv_is_bixu)
        TextView tv_is_bixu;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.iv_enter)
        ImageView iv_enter;
        @BindView(R.id.et_beizhu)
        EditText et_beizhu;
        @BindView(R.id.gv_pop_start_huli)
        GridView gv_pop_start_huli;
        @BindView(R.id.tv_selected_content)
        TextView tv_selected_content;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
